Repository navigation
[MRG] Fix batched envelope gradients with respect to marginals - #926
Open
xihaian251 wants to merge 1 commit into
Open
xihaian251 wants to merge 1 commit into
xihaian251 wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #926 +/- ##
==========================================
+ Coverage 97.09% 97.10% +0.01%
==========================================
Files 130 130
Lines 27229 27336 +107
==========================================
+ Hits 26437 26544 +107
Misses 792 792 🚀 New features to boost your workflow:
|
4 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Types of changes
Motivation and context / Related issue
solve_batch(..., grad="envelope")detaches the transport planTbefore evaluating the objective. The returnedvaluetherefore keeps a differentiable path to the cost matrixM(sograd_M = P*is correct), but the marginalsaandbonly enter through the detached solver. With entropy regularization their gradient wasNone; with KL regularization the only surviving term was an incorrect one coming from the reference measurea (x) b.Minimal reproducer (
M = 0,reg = 1,method="log_sinkhorn",grad="envelope",torch.float64,a=(p, 1-p),b=(1/2, 1/2),p=3/4):value-1.2554823251787532-1.2554823251787537(Wolfram)grad_MP*P*(already correct)grad_pNonelog 3 = 1.0986122886681097The same defect affects a second marginal: with
b=(q, 1/3, 2/3-q)andq=1/2,dF/dq = log 3, which the pre-fix API also failed to provide. The non-batchedot.solve(..., grad="envelope")already registers the correct sensitivitiesgrad_a = reg * (u - mean(u))andgrad_b = reg * (v - mean(v))throughnx.set_gradients. That helper implements a scalar-output VJP and is not valid for the(B,)vector value returned bysolve_batch: we verified a silent mis-broadcast atB=2and a shape error atB=3. This PR therefore adds a local, batch-aware autograd Function instead of changing the global backend API.How has this been tested (if it applies)
B=1andB=2/3/8. Before the fix,grad_pwasNonewhilegrad_M = w_b P_b*was already correct; after the fix both marginal and cost gradients match the analytic values.a=(p,1-p),b=(q,1/3,2/3-q),M=0,reg=1,p=3/4,q=1/2. A singletorch.autograd.gradcall checksgrad_p = grad_q = w * log 3, together with shape, dtype, device and upstream-cotangent scaling.log_sinkhorn,sinkhornandproximal, float32/float64 and near-zero marginals. The explicit-reg log(a)KL reference term is included, and the proximal effective coefficient isreg + inner_reg.POT_BACKEND_DISABLE_PYTORCH=1,import ot,import ot.batchand NumPysolve_batch(entropy/KL, envelope/detach/autodiff) all succeed, with noFalse.autogrador undefined-class errors.pytest -q test/batch/— 132 passed, 3 pre-existing warnings. Ruff 0.5.2 (the version pinned by pre-commit) andgit diff --checkare clean.pytest -q test/ --ignore=test/test_bspot.py --ignore=test/test_helpers.pygave 3614 passed, 468 skipped, 6 xfailed, exit code 0. The two exclusions are documented limitations, not passing tests: (1) the locally available BSP native extension was compiled from a different source revision than this master, causing a segfault intest/test_bspot.py; (2) the sandbox blocksreg.exe, so MSVC cannot probe the Windows SDK andtest/test_helpers.pyfails at compiler detection. We did not bypass the security policy, and we do not claim the unmodified full suite is green. Upstream CI has not run yet for this PR.PR checklist
Scope note
The change is local to
ot/batch/_linear.pyand applies to PyTorch only. It preserves the existingMgradient path and the forwardvalueandplanbit-for-bit, and leaves JAX/TensorFlow behaviour unchanged. The custom marginal gradient is first-order only; higher-order differentiation, JAX/TensorFlow batch marginal gradients andsolve_gromov_batchenvelope gradients are explicitly out of scope.